package com.stu.springmvc.servlet;

import javax.jws.soap.SOAPBinding;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyServlet extends HttpServlet {

    public void init(){
        System.out.println("this is init method");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handleLogic(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        handleLogic(req, resp);
    }

    private void handleLogic(HttpServletRequest req, HttpServletResponse resp){
        System.out.println("handle myLogic");
        ServletContext sc = getServletContext();
        RequestDispatcher rd = null;
        // 页面跳转
        rd = sc.getRequestDispatcher("/index.jsp");
        try {
            rd.forward(req, resp);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
